package com.yingpai.fitness.dvp_2_0.main.activity.fitness;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MyLocationConfiguration;
import com.baidu.mapapi.map.MyLocationData;
import com.baidu.mapapi.model.LatLng;
import com.orhanobut.logger.Logger;
import com.yingpai.fitness.R;
import com.yingpai.fitness.dvp_2_0.common.activity.CommonActivity;
import com.yingpai.fitness.dvp_2_0.common.util.constant.Contacts;
import com.yingpai.fitness.dvp_2_0.main.util.WechatLoginUtil;
import com.yingpai.fitness.entity.store.CourseDetailBean;
import com.yingpai.fitness.entity.store.PersonalDetailBean;

import java.net.URISyntaxException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by ASUS on 2018/8/14.
 */

public class MapActivity extends CommonActivity {

    @BindView(R.id.bmapView)
    MapView bmapView;
    @BindView(R.id.title_bar)
    RelativeLayout titleBar;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.address)
    TextView address;
    private BaiduMap mBaiduMap;
    //    private MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.COMPASS;//罗盘
//    private MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING;//定位跟随态
    private MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;   //默认为 LocationMode.NORMAL 普通态
    private BitmapDescriptor mCurrentMarker;
    private CourseDetailBean.MapData.StoreData storeData_course;
    private PersonalDetailBean.MapData.StoreData storeData_personal;
    private String flag;
    private double latitude;
    private double longitude;
    private LatLng point;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStatusFlag(Contacts.NORMAL_STATUS);
        setContentView(R.layout.activity_map);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        bmapView.onDestroy();
    }

    @Override
    protected void initView() {
        titleBar.setBackgroundColor(getResources().getColor(R.color.transparent));

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            flag = bundle.getString("flag");
            if ("course".equals(flag)) {
                storeData_course = (CourseDetailBean.MapData.StoreData) bundle.getSerializable("storeData");
                name.setText(storeData_course.getStoreName());
                address.setText(storeData_course.getAddress());
                latitude = Double.parseDouble(storeData_course.getLatitude());
                longitude = Double.parseDouble(storeData_course.getLongitude());
            } else {
                storeData_personal = (PersonalDetailBean.MapData.StoreData) bundle.getSerializable("storeData");
                name.setText(storeData_personal.getStoreName());
                address.setText(storeData_personal.getAddress());
                latitude = Double.parseDouble(storeData_personal.getLatitude());
                longitude = Double.parseDouble(storeData_personal.getLongitude());
            }
        }

        mBaiduMap = bmapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);

        // 开启定位图层
        mBaiduMap.setMyLocationEnabled(true);

        //定义Maker坐标点
        point = new LatLng(latitude, longitude);

        // 设置定位图层的配置（定位模式，是否允许方向信息，用户自定义定位图标）
        mCurrentMarker = BitmapDescriptorFactory
                .fromResource(R.mipmap.location);

        // 设置地图的缩放级别
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(15));// 默认是12
        // 设置地图中心点
        mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));

        // 构造定位数据
        MyLocationData locData = new MyLocationData.Builder()
//                .accuracy(1)
                // 此处设置开发者获取到的方向信息，顺时针0-360
//                .direction(0)
                .latitude(latitude)
                .longitude(longitude)
                .build();

        // 设置定位数据
        mBaiduMap.setMyLocationData(locData);

        MyLocationConfiguration config = new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
        mBaiduMap.setMyLocationConfiguration(config);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        bmapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        bmapView.onPause();
    }

    @Override
    @OnClick({R.id.location, R.id.navigation})
    public void onViewClicked(View view) {
        super.onViewClicked(view);
        switch (view.getId()) {
            case R.id.location:
                // 设置地图中心点
                mBaiduMap.setMapStatus(MapStatusUpdateFactory.newLatLng(point));
                break;
            case R.id.navigation:
                if (WechatLoginUtil.getInstance().isAvilible(this, "com.baidu.BaiduMap")) {
                    try {
                        Intent intent = Intent.getIntent("intent://map/direction?destination=latlng:"
                                + latitude + "," + longitude + "|name:" + address.getText().toString() + // 终点
                                "&mode=driving&" + // 导航路线方式
                                "region=北京" + //
                                "&src=新疆和田#Intent;scheme=bdapp;package=com.baidu.BaiduMap;end");
                        startActivity(intent); // 启动调用
                    } catch (URISyntaxException e) {
                        Logger.e(e.getMessage());
                    }
                } else if (WechatLoginUtil.getInstance().isAvilible(this, "com.autonavi.minimap")) {
                    Intent intent = null;
                    try {
                        intent = Intent.getIntent("androidamap://navi?sourceApplication=新疆和田&poiname=" + address.getText().toString()
                                + "&lat=" + latitude
                                + "&lon=" + longitude + "&dev=0");
                    } catch (URISyntaxException e) {
                        e.printStackTrace();
                    }
                    startActivity(intent);

                } else if (WechatLoginUtil.getInstance().isAvilible(this, "com.google.android.apps.maps")) {
                    Uri gmmIntentUri = Uri.parse("google.navigation:q="
                            + latitude + "," + longitude
                            + ", + Sydney +Australia");
                    Intent mapIntent = new Intent(Intent.ACTION_VIEW,
                            gmmIntentUri);
                    mapIntent.setPackage("com.google.android.apps.maps");
                    startActivity(mapIntent);

                } else {
                    showToast("您未安装任何导航工具");
                }
                break;
        }
    }
}

